package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppRoomPaimaiService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppRoomPaimai;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房间排麦
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppRoomPaiMaiController.java
 * @Description TODO
 * @createTime 2021年07月29日 14:55:00
 */
@CrossOrigin
@Controller
@RequestMapping("/app/roompaimai")
public class AppRoomPaiMaiController extends ClientBaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AppRoomPaimaiService appRoomPaimaiService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AppUserService appUserService;

    /**
    * 房间排麦列表
    * @Param: [roomid 房间id  pageno 页码]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/29
    * @Time: 15:26
    */
    @RequestMapping("/list")
    @ResponseBody
    public Result list(Integer roomid,Integer pageno){
        try{
            if (pageno == null || pageno <= 0) {
                return this.errorJSON("没有更多数据了！");
            }
            SplitPageBean splitPage = new SplitPageBean(pageno, 8);
            Map<String,Object> map = new HashMap<>(16);
            map.put("roomid",roomid);
            List<AppRoomPaimai> list = appRoomPaimaiService.list(map,splitPage);
            //判断数据是否为空
            if (list != null && list.size() > 0){
                return this.sucessPage(list, splitPage.getNextPageNo());
            }else{
                //为空下一页标识为：-1
                return this.sucessPage(list, -1);
            }
        }catch (Exception e){
            logger.error("排麦列表异常",e);
            return this.errorJSON("系统繁忙");
        }
    }


    /**
    * 申请上麦
    * @Param: [roomid 房间id]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/29
    * @Time: 15:01
    */
    @RequestMapping("/shenqing")
    @ResponseBody
    public Result shenqing(Integer roomid){
        try{
            if(StringEx.isNull(roomid)){
                return this.errorJSON("未获取到房间信息");
            }
            //获取最新数据
            Integer userId = (Integer)redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser user =appUserService.get(userId);
            AppRoomPaimai roomPaimai = appRoomPaimaiService.getRoom(roomid,user.getId());
            if(StringUtils.isEmpty(roomPaimai)){
                AppRoomPaimai appRoomPaimai = new AppRoomPaimai();
                appRoomPaimai.setRoomid(roomid);
                appRoomPaimai.setUserid(user.getId());
                appRoomPaimai.setNick(user.getNick());
                appRoomPaimai.setPic(user.getPic());
                appRoomPaimai.setPhone(user.getLoginname());
                appRoomPaimai.setSex(user.getSex());
                appRoomPaimai.setState(1);//申请上麦
                appRoomPaimai.setCreatetime(new Date());
                appRoomPaimaiService.save(appRoomPaimai);
            }else{
                return this.sucessJSON("已申请上麦");
            }
            return this.sucessJSON("申请成功");
        }catch (Exception e){
            logger.error("申请上麦异常",e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
    * 取消上麦，拒绝上麦
    * @Param: [id 排麦id]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/29
    * @Time: 15:09
    */
    @RequestMapping("/quxiao")
    @ResponseBody
    public Result quxiao(Integer id){
        try{
            if(StringEx.isNull(id)){
                return this.errorJSON("未找到相关麦位信息");
            }
            AppRoomPaimai roomPaimai = appRoomPaimaiService.get(id);
            if(StringUtils.isEmpty(roomPaimai)){
                return this.errorJSON("未找到相关麦位信息");
            }
            //删除排麦的麦位信息
            appRoomPaimaiService.del(roomPaimai.getRoomid(),roomPaimai.getUserid());
            return this.sucessJSON("取消成功");
        }catch (Exception e){
            logger.error("取消上麦异常",e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
    * 同意上麦
    * @Param: [id]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/29
    * @Time: 15:12
    */
    @RequestMapping("/tongyi")
    @ResponseBody
    public Result tongyi(Integer id){
        try{
            if(StringEx.isNull(id)){
                return this.errorJSON("未获取到用户上麦信息");
            }
            //修改上麦状态 同意上麦
            appRoomPaimaiService.updateState(id,2);
            return this.sucessJSON("上麦成功");
        }catch (Exception e){
            logger.error("同意用户上麦时异常",e);
            return this.errorJSON("系统繁忙");
        }
    }



}
